Functions (FC)

Functions (FCs) belong to the blocks that you program yourself. A function is a logic block "without memory." Temporary variables belonging to the FC are saved in the local data stack. This data is then lost when the FC has been executed. To save data permanently, functions can also use shared data blocks.

Since an FC does not have any memory of its own, you must always specify actual parameters for it. You cannot assign initial values for the local data of an FC.

Application
An FC contains a program section that is always executed when the FC is called by a different logic block. You can use functions for the following purposes:

  • To return a function value to the calling block (example: math functions)
  • To execute a technological function (example: single control function with a bit logic operation).

Assigning Actual Parameters to the Formal Parameters
A formal parameter is a dummy for the "actual" parameter. Actual parameters replace the formal parameters when the function is called. You must always assign actual parameters to the formal parameters of an FC (for example, an actual parameter "I 3.6" to the formal parameter "Start"). The input, output and in/out parameters used by the FC are saved as pointers to the actual parameters of the logic block that called the FC.

Important Differences Between the Output Parameters of FCs and FBs:

In function blocks (FB), a copy of the actual parameters in the instance DB is used when accessing the parameters. If an input parameter is not transferred or an output parameter is not write accessed when a FB is called, the older values still stored in the instance DB /Instance DB = memory of the FBs) will be used. Functions (FC) have no memory. Contrary to FBs, the assignment of formal parameters to these FCs is therefore not optional, but rather essentially. FC parameters are accessed via addresses (pointers to targets across area boundaries). When an address of the data area (data block) or a local variable of
the calling block is used as actual parameter, a copy of the actual parameter is saved temporarily to local data area of the calling block for the transfer of the parameter.

Thus, observe the following points:

  • If possible, initialize the OUTPUT parameters.
  • Set and reset instructions depend on RLO. When these instructions are used to determine the value at an OUTPUT parameter, no value is generated if the result of a previous logic operation (RLO) = 0.
  • Always ensure that data are written to the OUTPUT parameters - irrespective of any program paths in the block. Pay special attention to jump instructions, to the ENO output in LAD and FBD as well as to BEC (Block End Conditional) and the influence of MCR (Master Control Relay) instructions.

Note
Although the OUTPUT parameters of an FB or the INOUT parameters of an FC and FB will not output random values (the old output value - or input value as output value - is going to be maintained even if no data are written to the parameter) you should still observe the points above in order to avoid unintentional processing of "old" values.

Function Blocks (FB)
Function blocks (FBs) belong to the blocks that you program yourself. A function block is a block "with memory." It is assigned a data block as its memory (instance data block). The parameters that are transferred to the FB and the static variables are saved in the instance DB. Temporary variables are saved in the local data stack. Data saved in the instance DB are not lost when execution of the FB is complete. Data saved in the local data stack are, however, lost when execution of the FB is completed.

Note
To avoid errors when working with FBs, read Permitted Data Types when Transferring Parameters in the Appendix.

Application
An FB contains a program that is always executed when the FB is called by a different logic block. Function blocks make it much easier to program frequently occurring, complex functions.

Function Blocks and Instance Data Blocks
An instance data block is assigned to every function block call that transfers parameters. By calling more than one instance of an FB, you can control more than one device with one FB. An FB for a motor type, can, for example, control various motors by using a different set of instance data for each different motor. The data for each motor (for example, speed, ramping, accumulated operating time etc.) can be
saved in one or more instance DBs.

The following figure shows the formal parameters of an FB that uses the actual parameters saved in the instance DB.

Variables of the Data Type FB
If your user program is structured so that an FB contains calls for further already existing function blocks, you can include the FBs to be called as static variables of the data type FB in the variable declaration table of the calling FB. This technique allows you to nest variables and concentrate the instance data in one instance data block (multiple instance).

Assigning Actual Parameters to the Formal Parameters
It is not generally necessary in STEP 7 to assign actual parameters to the formal parameters of an FB. There are, however, exceptions to this. Actual parameters must be assigned in the following situations:

  • For an in/out parameter of a complex data type (for example, STRING, ARRAY or DATE_AND_TIME)
  • For all parameter types (for example TIMER, COUNTER, or POINTER)

STEP 7 assigns the actual parameters to the formal parameters of an FB as follows:

  • When you specify actual parameters in the call statement: the instructions of the
  • FB use the actual parameters provided.
  • x When you do not specify actual parameters in the call statement: the
  • instructions of the FB use the value saved in the instance DB.